def length_of_longest_substring(s):
    n = len(s)
    if n <= 1:
        return n

    last_occurrence = {}
    start = 0
    max_length = 0

    for i in range(n):
        if s[i] not in last_occurrence or last_occurrence[s[i]] < start:
            max_length = max(max_length, i - start + 1)
        else:
            start = last_occurrence[s[i]] + 1
        last_occurrence[s[i]] = i

    return max_length
s1 = "abcabcbb"
print(length_of_longest_substring(s1))  # 输出：3

s2 = "bbbbb"
print(length_of_longest_substring(s2))  # 输出：1

s3 = "pwwkew"
print(length_of_longest_substring(s3))  # 输出：3
